package com.example.mvcdemo1.controller;

import com.example.mvcdemo1.model.Student;
import com.example.mvcdemo1.service.StudentService;
import com.example.mvcdemo1.service.StudentServiceLmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/")
    public String viewHomePage(Model model){
        model.addAttribute("listStudent",studentService.getAllStudents());
        return "index";
    }

    @GetMapping("/showNewStudentForm")
    public String showStudet(Model model){
        Student student = new Student();
        model.addAttribute("student",student);
        return "new_student";
    }

    @GetMapping("/saveStudent")
    public String saveStudnt(@ModelAttribute("student") Student student){
        studentService.saveStudent(student);
        return "redirect:/";
    }

    @GetMapping("/showNewStudentUpdate{id}")
    public String showFormForUpdate(@PathVariable(value = "id") long id,Model model){
        Student student = studentService.getStudentByid(id);

        model.addAttribute("student",student);
        return "update_student";
    }

    @GetMapping("/deleteStudent{id}")
    public String deleteStudent(@PathVariable(value = "id") long id){
        this.studentService.deleteStudentByid(id);
        return "redirect:/";
    }
}
